home *** CD-ROM | disk | other *** search
/ SGI Hot Mix 14 / Hot Mix 14.iso / .all / bin / netscape / Slider.java (.txt) < prev    next >
Text File  |  1996-07-21  |  10KB  |  289 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.lang.Object;
  4. import java.awt.*;
  5. import java.net.*;
  6.  
  7. // Slider.java 
  8. // Steve Green 1996
  9.  
  10.  
  11. // This is a simple wood block slider game. It will display up to an 8 * 8
  12. // grid of blocks depending on how much room is allocated for the applet.
  13. // NOTE: Each block is 48*48
  14.  
  15. // Thanks to Joe Carlson (j.carlson10@genie.geis.com or jcarlson@ecs.fullerton.edu 
  16. // for both the inspiration and the wonderful wood block images.
  17.  
  18. public class Slider extends Applet implements Runnable {
  19.         Tile grid[][];
  20.         public static AudioClip clinkSound,introSound,winSound,badSound;
  21.         public boolean intro = true, random = false;
  22.         Dimension GridSize;
  23.         Point blank;
  24.         Thread kicker = null;
  25.  
  26.         public void init() {
  27.                 int x,y,block=1,maxBlocks;
  28.                 String name;
  29.                 
  30. //              Set height and width
  31.  
  32.                 GridSize = new Dimension(size().width/48,(size().height-50)/48);
  33.                 
  34.                 grid = new Tile[GridSize.height][GridSize.width];
  35.                 
  36. //              Layout the applet
  37.  
  38.                 setLayout(new BorderLayout());
  39.                 resize(size().width,size().height);
  40.                 Panel p = new Panel();
  41.                 add("South",p);
  42.                 p.add(new Button("Randomize"));
  43.  
  44. //              Load sounds...
  45.  
  46.                 try {
  47.                         introSound = getAudioClip(new URL(getDocumentBase(),"sounds/spacemusic.au"));
  48.                         introSound.loop();
  49.                         clinkSound = getAudioClip(new URL(getDocumentBase(),"sounds/clink.au"));
  50.                         winSound = getAudioClip(new URL(getDocumentBase(),"sounds/laugh.au"));
  51.                 badSound = getAudioClip(new URL(getDocumentBase(),"sounds/crash.au"));
  52.                 } catch ( java.net.MalformedURLException e) {}
  53.  
  54. //              Load the blocks...
  55.  
  56.                 maxBlocks = GridSize.height * GridSize.width;           
  57.                 
  58.                 for (y=0;y<GridSize.height;y++) 
  59.                         for (x=0;x<GridSize.width;x++) {
  60.                                 if ( block < maxBlocks ) {
  61.                                         name = "graphics/" + block + ".gif";
  62.                                         grid[y][x] = new Tile(getImage(getCodeBase(),name),block,name);
  63.                                         block++;
  64.                                 }
  65.                                 else {
  66.                                         grid[x][y] = new Tile(null,0,null);
  67.                                         blank = new Point(x,y);
  68.                                 }
  69.                 }
  70.                 repaint();
  71.         }
  72.  
  73.         public boolean action(Event evt, Object arg) {
  74.  
  75.                 if ( arg.equals("Randomize") ) {
  76.                         random = true;
  77.                         repaint();
  78.                 }
  79.  
  80.                 return true;
  81.         }
  82.  
  83.         private void randomize(Graphics g){
  84.  
  85.                 int i,iter=GridSize.height*GridSize.width*GridSize.width;
  86.                 Point from;
  87.                 
  88.                 for (i=0; i<iter;i++) {
  89.                         if ( Math.random() > 0.5 )
  90.                                 move(new Point(blank.x,(int)(Math.random() * GridSize.height)));
  91.                         else
  92.                                 move(new Point((int)(Math.random() * GridSize.width),blank.y));
  93.  
  94.                         drawGrid(g);
  95.                 }
  96.         }
  97.                                 
  98.         public void run() {
  99.                 Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  100.                 while (kicker != null) {
  101. //                      repaint();
  102.                         try {
  103.                                 Thread.sleep(100);
  104.                         } catch (InterruptedException e){}
  105.                 }
  106.         }
  107.  
  108.         private void drawAll() {
  109.                 int x,y;
  110.  
  111.                 for (y=0;y<GridSize.height;y++)
  112.                         for (x=0;x<GridSize.width;x++)
  113.                                 grid[y][x].changed = true;
  114.                 repaint();
  115.         }
  116.  
  117.         public void paint(Graphics g) {
  118.  
  119.                 drawAll();
  120.         }
  121.  
  122.         public void start() {
  123.                 if ( kicker == null ) {
  124.                         kicker = new Thread(this);
  125.                         kicker.start();
  126.                 }
  127.         }
  128.  
  129.         public void stop() {
  130.                 if ( intro )
  131.                         introSound.stop();
  132.                 kicker = null;
  133.         }
  134.  
  135.         public void update(Graphics g) {
  136.  
  137.                 if ( random )
  138.                         randomize(g);
  139.                 else
  140.                         drawGrid(g);
  141.  
  142.                 random = false;
  143.         }
  144.  
  145.         private void drawGrid(Graphics g) {
  146.                 int x,y;
  147.  
  148. //              Only draw blocks that have changed to improve display speed.
  149.  
  150.                 for (y=0;y<GridSize.height;y++) 
  151.                         for (x=0;x<GridSize.width;x++)  {
  152.                                 if ( grid[y][x].changed ) {
  153.                                         if ( grid[y][x].number != 0 ) 
  154.                                                 g.drawImage(grid[y][x].img,x*48,y*48,this);
  155.                                         else {
  156.                                                 g.clearRect(x*48,y*48,48,48);
  157.                                         }
  158.                                         grid[y][x].changed = false;
  159.                                 }
  160.                         }
  161.         }
  162.  
  163.         public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
  164.         // When image is drawn, redraw THIS BLOCK.
  165.         // This is done by first finding where the block in the grid.
  166.         // THIS IS A BIT CRUDE!
  167.  
  168.                 int iy,ix;
  169.  
  170.                 if ((flags & (SOMEBITS|FRAMEBITS|ALLBITS)) != 0) {
  171. //              if ((flags & ALLBITS) != 0) {
  172.                         for (iy=0;iy<GridSize.height;iy++)
  173.                                 for (ix=0;ix<GridSize.width;ix++)
  174.                                         if ( grid[iy][ix].img == img ) {
  175.                                                 grid[iy][ix].changed = true;
  176.                                                 repaint(100);
  177.                                                 return (flags & (ALLBITS|ERROR)) == 0;
  178.                                         }
  179.                 }
  180.                 return (flags & (ALLBITS|ERROR)) == 0;
  181.         }
  182.  
  183.         private boolean solved() {
  184.  
  185. //      Return true if the puzzle is solved, otherwise false
  186.  
  187.                 int  y,x,block=1,iter = GridSize.height * GridSize.width;
  188.                 for (y=0;y<GridSize.height;y++)
  189.                         for (x=0;x<GridSize.width;x++)
  190.                                 if ( block < iter )
  191.                                         if ( grid[y][x].number != block++ )
  192.                                                 return false;
  193.                 return true;
  194.         }
  195.  
  196.         private int delta(int a,int b) {
  197.  
  198. //      Return the direction to move the block in
  199.  
  200.                 if ( a == b ) return 0;
  201.                 else return((b-a)/Math.abs(b-a));
  202.         }
  203.  
  204.         public boolean mouseDown(Event evt, int x, int y) {
  205.  
  206. //      Mouse clicked somewhere
  207.  
  208.                 Point from;     
  209.  
  210. //      Translate to block coordinates
  211.  
  212.                 from = new Point(x/48,y/48);
  213.  
  214. //      Kill the intro sound if still playing...
  215.  
  216.                 if ( intro )
  217.                         introSound.stop();
  218.                 intro = false;
  219.  
  220.                 if ( from.x >= GridSize.width || from.y >= GridSize.height )
  221.                         return true;
  222.  
  223. //      If on the same row or column as the blank we can move
  224.  
  225.                 if ( move(from) ) {
  226.                         clinkSound.play();
  227.  
  228. //                      Wait for about 250ms so that the sound and the
  229. //                      animation appear to be in sync.
  230.  
  231.                         try {
  232.                                 Thread.sleep(250);
  233.                         } catch (InterruptedException e) {}
  234.                         repaint();
  235.                         if ( solved() ) 
  236.                                 winSound.play();
  237.                 }
  238.                 else 
  239.                         badSound.play();
  240.  
  241.                 return true;
  242.         }
  243.  
  244.         private boolean move(Point from) {
  245.           Point to,by;
  246.           
  247.                 by = new Point(delta(from.x,blank.x),delta(from.y,blank.y));
  248.           
  249.                 if ( (by.x == 0 && by.y == 0) || (by.x != 0 && by.y != 0) )
  250.                         return false;
  251.  
  252.                 to = new Point(from.x + by.x, from.y + by.y);
  253.  
  254. //      Try and move, if blocked by another block, move it first...
  255.  
  256.                 if ( grid[to.y][to.x].number != 0 ) 
  257.                         move(to);
  258.  
  259. //      Move the block and repaint
  260.  
  261.                 grid[to.y][to.x].number = grid[from.y][from.x].number;  // to here...
  262.                 grid[to.y][to.x].img = grid[from.y][from.x].img;
  263.                 grid[to.y][to.x].changed = true;
  264.  
  265.                 grid[from.y][from.x].number = 0;                        // from here...
  266.                 grid[from.y][from.x].img = null;
  267.                 grid[from.y][from.x].changed = true;
  268.  
  269.                 blank = from;                           // blank is now here...
  270.                 return true;
  271.         }
  272. }
  273.  
  274. class Tile {
  275.   Image img=null;
  276.   int number=0;
  277.   String name;
  278.   boolean changed=true;
  279.  
  280.         public Tile(Image theImage,int i,String imageName) {
  281.  
  282.                 name = imageName;
  283.                 number = i;
  284.                 img =  theImage;
  285.         }
  286. }
  287.  
  288.  
  289.